import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class WarehouseFunctionalTest(unittest.TestCase):
    
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(10)
        cls.driver.get("http://localhost:5000")  # 假设应用运行在本地5000端口
        # 登录操作等...
    
    def test_low_stock_alert_page(self):
        """测试库存预警页面"""
        self.driver.find_element(By.LINK_TEXT, "库存预警").click()
        
        # 检查页面标题
        title = self.driver.find_element(By.TAG_NAME, "h1").text
        self.assertEqual(title, "库存预警列表")
        
        # 检查表格数据
        rows = self.driver.find_elements(By.CSS_SELECTOR, "#low-stock-table tbody tr")
        self.assertGreater(len(rows), 0)
        
        # 测试筛选功能
        threshold_input = self.driver.find_element(By.ID, "threshold-filter")
        threshold_input.clear()
        threshold_input.send_keys("10")
        threshold_input.send_keys(Keys.RETURN)
        
        WebDriverWait(self.driver, 5).until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR, "#low-stock-table tbody tr:first-child td:nth-child(5)"), 
                "8"  # 当前库存
            )
        )
        
        rows = self.driver.find_elements(By.CSS_SELECTOR, "#low-stock-table tbody tr")
        self.assertEqual(len(rows), 1)  # 只有一个商品库存低于10
    
    def test_product_search_page(self):
        """测试商品查询页面"""
        self.driver.find_element(By.LINK_TEXT, "商品查询").click()
        
        # 测试搜索功能
        search_input = self.driver.find_element(By.ID, "product-search")
        search_input.send_keys("测试商品1")
        search_input.send_keys(Keys.RETURN)
        
        WebDriverWait(self.driver, 5).until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR, "#product-table tbody tr:first-child td:nth-child(2)"), 
                "测试商品1"
            )
        )
        
        rows = self.driver.find_elements(By.CSS_SELECTOR, "#product-table tbody tr")
        self.assertEqual(len(rows), 1)
    
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

if __name__ == '__main__':
    unittest.main()
